package com.onesports.intelligent.k12.polarlight.controller.wx.user;

import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.onesports.intelligent.k12.polarlight.common.utils.ResultUtil;
import com.onesports.intelligent.k12.polarlight.constant.ApiPathConstants;
import com.onesports.intelligent.k12.polarlight.domain.dto.pay.PayRequestDTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.sys.WxUser;
import com.onesports.intelligent.k12.polarlight.domain.vo.market.FundsTradeBillVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.sys.WxPayOrderStatusVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.sys.WxPayResultVO;
import com.onesports.intelligent.k12.polarlight.security.SecurityUtils;
import com.onesports.intelligent.k12.polarlight.service.order.PayService;
import com.onesports.intelligent.k12.polarlight.service.student.WxUserService;
import com.onesports.intelligent.k12.polarlight.service.wx.WxUserPurseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author Lenovo
 */
@Api(tags = "会员端-我的钱包")
@RestController
@RequiredArgsConstructor
@RequestMapping(ApiPathConstants.WX_USER_PURSE)
public class UserPurseController {

    private final WxUserService wxUserService;

    private final WxUserPurseService wxUserPurseService;

    private final PayService payService;

    @GetMapping("/trade-list")
    @ApiOperation(value = "财务流水")
    @ApiImplicitParam(name = "income", value = "是否查收入(全部：不传，收入：true,支出：false)", required = false, dataType = "boolean", paramType = "query")
    public ResponseEntity<List<FundsTradeBillVO>> tradeList(@RequestParam(required = false) Boolean income){
        String accountId = SecurityUtils.getAccountId();
        return ResultUtil.success(wxUserPurseService.tradeList(income,accountId));
    }

    @GetMapping("/recharge")
    @ApiOperation(value = "充值发起")
    @ApiImplicitParam(name = "amount", value = "充值金额(精度：小数点后两位)", required = true, dataType = "BigDecimal", paramType = "query")
    public ResponseEntity<WxPayResultVO> recharge(@NotBlank(message = "充值金额不能为空")BigDecimal amount) throws WxPayException {
        WxUser wxUser = wxUserService.getById(SecurityUtils.getAccountId());
        PayRequestDTO dto = PayRequestDTO.builder()
                .wxUserId(wxUser.getId())
                .profitSharing(false)
                .openid(wxUser.getOpenId())
                .tradeType(TradeTypeEnum.JSAPI)
                .timeExpire(DateUtils.addMinutes(new Date(), 30))
                .amount(amount)
                .description(com.onesports.intelligent.k12.polarlight.enums.TradeTypeEnum.RECHARGE.getLabel())
                .build();
        return ResultUtil.success(payService.createOrder(dto));
    }

    @GetMapping("/query-pay-status")
    @ApiOperation(value = "支付结果查询")
    @ApiImplicitParam(name = "payOrderNo", value = "支付订单号", required = true, dataType = "string", paramType = "query")
    public ResponseEntity<WxPayOrderStatusVO> queryPayStatus(String payOrderNo) throws WxPayException{
        return ResultUtil.success(payService.queryPayStatus(payOrderNo));
    }

    @GetMapping("/cash-out")
    @ApiOperation(value = "提现发起")
    @ApiImplicitParam(name = "amount", value = "提现金额(精度：小数点后两位)", required = true, dataType = "BigDecimal", paramType = "query")
    public ResponseEntity cashOut(@NotBlank(message = "提现金额不能为空")BigDecimal amount){
        return ResultUtil.error("功能建设中……");
    }


}
